<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<title>login</title>
	<link rel="stylesheet" href="../css/login.css" />
</head>

<!--  <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script> -->
<script src="../js/jquery-3.2.1.min.js"></script>
<script type="text/javascript">

	function loginForm() {
		var unindexed_array = $("form").serializeArray();
		var param = {};
		$.map(unindexed_array, function (n, i) {
			param[n['name']] = n['value'];
		});

		$.ajax({
			type: "post",
			contentType: "application/json",
			url: "http://localhost:8888/user/login",
			async: false,
			data: JSON.stringify(param),
			dataType: "json",
			success: function (data) {
				if (data.code == 1) {
					//	alert(data.message);
					sessionStorage.setItem('token', data.data);
					sessionStorage.setItem('username', $('#phone').val());
					window.location.replace("./main.html");

				} else {
					alert(data.message);
				}

			}, error: function (data) {
				alert(data.message);
			}
		});

	}

	$(function () {
		$.post("http://localhost:8888/user/verification", function (result) {
			$("img").attr("src", "data:image/png;base64," + result.data.value);
			$(".codeToken").val(result.data.token)
		}, "json")
	})


	function vercode() {
		$.post("http://localhost:8888/user/verification", function (result) {
			$("img").attr("src", "data:image/png;base64," + result.data.value);
			$(".codeToken").val(result.data.token);
		}, "json")
	}
</script>

<body>
	<div class="box">
		<h2>登录</h2>
		<form method="post">
			<div class="inputbox">
				<input type="text" name="phone" id="phone" required="" />
				<label>用户名</label>
			</div>
			<div class="inputbox">
				<input type="password" name="password" id="password" required="" />
				<label>密码</label>
			</div>
			<div class="inputbox">
				<input class="code" type="text" name="verificationCode" id="verificationCode" required="" />
				<label>验证码</label>
				<span>
					<img onclick="vercode()" />
				</span>
			</div>
			<input class="codeToken" type="text" name="verificationToken" id="verificationToken" hidden />
			<input class="sub" type="button" value="Login" style="margin-left:70px;" onclick="loginForm()" />
			<a class="sub but" href="./register.html">Register</a>
		</form>
	</div>
</body>

</html>